<template>
  <div id="app">
    <TopBar :key="topbarkey"></TopBar>
    <Header></Header>
    <router-view @updataTopbarKey="topbarkey++" />
    <Footer></Footer>
    <Login v-show="isShowLoginModal"></Login>
    <transition name="slide">
      <Toast v-show="isShowToast"></Toast>
    </transition>
  </div>
</template>
<script>
import Toast from "@/components/Toast.vue";
import TopBar from "./components/TopBar.vue";
import Footer from "./components/Footer.vue";
import Header from "./components/Header.vue";
import Login from "@/components/Login.vue";
import { JingpinAPI } from "@/request/api.js";
import { mapState } from "vuex";
export default {
  components: {
    TopBar,
    Footer,
    Header,
    Login,
    Toast,
  },
  data() {
    return {
      topbarkey: 0,
    };
  },
  async created() {
    // 缺点:
    //    1.服务器url地址都一样,每个组件调用都需要复制过去
    //     一旦接口地址发生改变,所有调用了接口的组件都需要修改
    // 2.每次请求时都需要手动编写config携带的token(多个组件携带代码重复)
    // 3.优化then()写法,async/await写法
    // JingpinAPI().then(
    //   (res) => {
    //     console.log("res2:", res); //在network中看  请求地址,响应结果
    //   },
    //   (err) => {
    //     console.log("err2:", err);
    //   }
    // );
    // let res = await JingpinAPI();
    // console.log("res3:", res);
    console.log("配置值为", process.env.VUE_APP_BASE_URL);
    console.log("配置值为", process.env.VUE_APP_MY_TEST);
  },
  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.showModal.isShowLoginModal,
      isShowToast: (state) => state.showToast.isShowToast,
    }),
  },
  watch: {
    "$route.path": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.topbarkey++;
        }
      },
    },
  },
};
</script>
<style lang="less">
#app {
  height: 100px;
  width: auto;
  // background-color: #cfc;
}
/* 入场的起始状态 = 离场的结束状态 */
.slide-enter,
.slide-leave-to {
  opacity: 0;
}

.slide-enter-active,
.slide-leave-active {
  transition: opacity 0.3s linear;
}

.slide-enter-to,
.slide-leave {
  opacity: 1;
}
</style>
